package com.gexin.weixin.controller;

import com.gexin.model.weixin.WeixinUser;
import com.gexin.weixin.kit.WxConfigKit;
import com.jfinal.weixin.iot.msg.InEquDataMsg;
import com.jfinal.weixin.iot.msg.InEqubindEvent;
import com.jfinal.weixin.sdk.api.ApiConfig;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.api.UserApi;
import com.jfinal.weixin.sdk.jfinal.MsgControllerAdapter;
import com.jfinal.weixin.sdk.msg.in.*;
import com.jfinal.weixin.sdk.msg.in.card.*;
import com.jfinal.weixin.sdk.msg.in.event.*;
import com.jfinal.weixin.sdk.msg.in.speech_recognition.InSpeechRecognitionResults;
import com.jfinal.weixin.sdk.msg.out.OutCustomMsg;
import com.jfinal.weixin.sdk.msg.out.OutTextMsg;
import com.jfinal.weixin.sdk.msg.out.OutVoiceMsg;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 *   微信事件通讯  微信开发者打开的时候 设置参数  http://xxxx.com/weixin/event/message?appid=?   appid 就是数据库自动增长的id
 */
public class WeixinEventMsgController extends MsgControllerAdapter {
	static Logger logger = LogManager.getLogger(WeixinEventMsgController.class);

	public ApiConfig getApiConfig() {
		return WxConfigKit.getApiConfig(getParaToInt("appid"));
	}


	@Override
	protected void processInTextMsg(InTextMsg inTextMsg) {
		OutCustomMsg outCustomMsg = new OutCustomMsg(inTextMsg);
		render(outCustomMsg);
	}

	@Override
	protected void processInVoiceMsg(InVoiceMsg inVoiceMsg) {
		// 转发给多客服PC客户端
		// OutCustomMsg outCustomMsg = new OutCustomMsg(inVoiceMsg);
		// render(outCustomMsg);
		OutVoiceMsg outMsg = new OutVoiceMsg(inVoiceMsg);
		// 将刚发过来的语音再发回去
		outMsg.setMediaId(inVoiceMsg.getMediaId());
		render(outMsg);
	}

	@Override
	protected void processInVideoMsg(InVideoMsg inVideoMsg) {
		/*
		 * 腾讯 api 有 bug，无法回复视频消息，暂时回复文本消息代码测试 OutVideoMsg outMsg = new OutVideoMsg(inVideoMsg); outMsg.setTitle("OutVideoMsg 发送"); outMsg.setDescription("刚刚发来的视频再发回去"); // 将刚发过来的视频再发回去，经测试证明是腾讯官方的 api 有 bug，待 api bug 却除后再试 outMsg.setMediaId(inVideoMsg.getMediaId()); render(outMsg);
		 */
		OutTextMsg outMsg = new OutTextMsg(inVideoMsg);
		outMsg.setContent("\t视频消息已成功接收，该视频的 mediaId 为: " + inVideoMsg.getMediaId());
		render(outMsg);
	}

	@Override
	protected void processInShortVideoMsg(InShortVideoMsg inShortVideoMsg) {
		OutTextMsg outMsg = new OutTextMsg(inShortVideoMsg);
		outMsg.setContent("\t视频消息已成功接收，该视频的 mediaId 为: " + inShortVideoMsg.getMediaId());
		render(outMsg);
	}

	@Override
	protected void processInLocationMsg(InLocationMsg inLocationMsg) {
		OutTextMsg outMsg = new OutTextMsg(inLocationMsg);
		outMsg.setContent("已收到地理位置消息:" + "\nlocation_X = " + inLocationMsg.getLocation_X() + "\nlocation_Y = " + inLocationMsg.getLocation_Y() + "\nscale = " + inLocationMsg.getScale() + "\nlabel = " + inLocationMsg.getLabel());
		render(outMsg);
	}

	@Override
	protected void processInLinkMsg(InLinkMsg inLinkMsg) {
		// 转发给多客服PC客户端
		OutCustomMsg outCustomMsg = new OutCustomMsg(inLinkMsg);
		render(outCustomMsg);
	}

	@Override
	protected void processInCustomEvent(InCustomEvent inCustomEvent) {
		logger.debug("测试方法：processInCustomEvent()");
		renderNull();
	}

	@Override
	protected void processInImageMsg(InImageMsg inImageMsg) {
		// 转发给多客服PC客户端
		OutCustomMsg outCustomMsg = new OutCustomMsg(inImageMsg);
		render(outCustomMsg);
/*		String picUrl = inImageMsg.getPicUrl();
		String respContent = picUrl;
		renderOutTextMsg(respContent);*/
	}

	/**
	 * 实现父类抽方法，处理关注/取消关注消息
	 */
	@Override
	protected void processInFollowEvent(InFollowEvent inFollowEvent) {
		if (InFollowEvent.EVENT_INFOLLOW_SUBSCRIBE.equals(inFollowEvent.getEvent())) {
			logger.debug("关注：" + inFollowEvent.getFromUserName());
			OutTextMsg outMsg = new OutTextMsg(inFollowEvent);
			outMsg.setContent("感谢关注！");
			ApiResult apiResult = UserApi.getUserInfo(inFollowEvent.getFromUserName());
			if(apiResult.isSucceed()){
				WeixinUser  user =  WeixinUser.dao.paseWxUser(apiResult);
				try {
					user.setAppid(getParaToInt("appid"));
					user.save();
				} catch (Exception e) {
					logger.error("关注保存NiacknameError");
					user.setNickname("none");
					user.save();
				}
			}
			//render(outMsg);
			renderNull();
		}
		// 如果为取消关注事件，将无法接收到传回的信息
		if (InFollowEvent.EVENT_INFOLLOW_UNSUBSCRIBE.equals(inFollowEvent.getEvent())) {
			WeixinUser.dao.update_follow(inFollowEvent.getFromUserName(),WeixinUser.Subscribe_Cancel);
			logger.debug("取消关注：" + inFollowEvent.getFromUserName());
			renderNull();
		}
	}

	@Override
	protected void processInQrCodeEvent(InQrCodeEvent inQrCodeEvent) {
		logger.info("扫码.......");
		if (InQrCodeEvent.EVENT_INQRCODE_SUBSCRIBE.equals(inQrCodeEvent.getEvent())) {
			logger.debug("扫码未关注：" + inQrCodeEvent.getFromUserName());
			OutTextMsg outMsg = new OutTextMsg(inQrCodeEvent);
			outMsg.setContent("感谢您的关注，二维码内容：" + inQrCodeEvent.getEventKey());
			render(outMsg);
			//把用户数据保存起来
			ApiResult apiResult = UserApi.getUserInfo(inQrCodeEvent.getFromUserName());
			if(apiResult.isSucceed()){
				WeixinUser  user =  WeixinUser.dao.paseWxUser(apiResult);
				try {
					user.setAppid(getParaToInt("appid"));
					user.save();
				} catch (Exception e) {
					logger.error("关注保存Niack年ameError");
					user.setNickname("none");
					user.save();
				}
			}
			renderNull();
		}
		if (InQrCodeEvent.EVENT_INQRCODE_SCAN.equals(inQrCodeEvent.getEvent())) {
			logger.debug("扫码已关注：" + inQrCodeEvent.getFromUserName());
			//renderOutTextMsg("扫码已关注,二维码内容：" + inQrCodeEvent.getEventKey());
			renderNull();
		}

	}

	@Override
	protected void processInLocationEvent(InLocationEvent inLocationEvent) {
		logger.debug("发送地理位置事件：" + inLocationEvent.getFromUserName());
		OutTextMsg outMsg = new OutTextMsg(inLocationEvent);
		outMsg.setContent("地理位置是：" + inLocationEvent.getLatitude());
		render(outMsg);
	}

	@Override
	protected void processInMassEvent(InMassEvent inMassEvent) {
		logger.debug("测试方法：processInMassEvent()");
		renderNull();
	}

	/**
	 * 实现父类抽方法，处理自定义菜单事件
	 */
	@Override
	protected void processInMenuEvent(InMenuEvent inMenuEvent) {
		logger.debug("菜单事件：" + inMenuEvent.getFromUserName());
		OutTextMsg outMsg = new OutTextMsg(inMenuEvent);
		outMsg.setContent("菜单事件内容是：" + inMenuEvent.getEventKey());
		render(outMsg);
	}

	@Override
	protected void processInSpeechRecognitionResults(InSpeechRecognitionResults inSpeechRecognitionResults) {
		logger.debug("语音识别事件：" + inSpeechRecognitionResults.getFromUserName());
		OutTextMsg outMsg = new OutTextMsg(inSpeechRecognitionResults);
		outMsg.setContent("语音识别内容是：" + inSpeechRecognitionResults.getRecognition());
		render(outMsg);
	}

	@Override
	protected void processInTemplateMsgEvent(InTemplateMsgEvent inTemplateMsgEvent) {
		logger.debug("测试方法：processInTemplateMsgEvent()");
		renderNull();
	}

	@Override
	protected void processInShakearoundUserShakeEvent(InShakearoundUserShakeEvent inShakearoundUserShakeEvent) {

	}

	@Override
	protected void processInVerifySuccessEvent(InVerifySuccessEvent inVerifySuccessEvent) {

	}

	@Override
	protected void processInVerifyFailEvent(InVerifyFailEvent inVerifyFailEvent) {

	}

	@Override
	protected void processInPoiCheckNotifyEvent(InPoiCheckNotifyEvent inPoiCheckNotifyEvent) {

	}

	@Override
	protected void processInWifiEvent(InWifiEvent inWifiEvent) {

	}

	@Override
	protected void processInUserCardEvent(InUserCardEvent inUserCardEvent) {

	}

	@Override
	protected void processInUpdateMemberCardEvent(InUpdateMemberCardEvent inUpdateMemberCardEvent) {

	}

	@Override
	protected void processInUserPayFromCardEvent(InUserPayFromCardEvent inUserPayFromCardEvent) {

	}

	@Override
	protected void processInMerChantOrderEvent(InMerChantOrderEvent inMerChantOrderEvent) {

	}

	@Override
	protected void processIsNotDefinedEvent(InNotDefinedEvent inNotDefinedEvent) {

	}

	@Override
	protected void processIsNotDefinedMsg(InNotDefinedMsg inNotDefinedMsg) {

	}

	@Override
	protected void processInUserGiftingCardEvent(InUserGiftingCardEvent inUserGiftingCardEvent) {

	}

	@Override
	protected void processInUserGetCardEvent(InUserGetCardEvent inUserGetCardEvent) {

	}

	@Override
	protected void processInUserConsumeCardEvent(InUserConsumeCardEvent inUserConsumeCardEvent) {

	}

	@Override
	protected void processInCardSkuRemindEvent(InCardSkuRemindEvent inCardSkuRemindEvent) {

	}

	@Override
	protected void processInCardPayOrderEvent(InCardPayOrderEvent inCardPayOrderEvent) {

	}

	@Override
	protected void processInCardPassCheckEvent(InCardPassCheckEvent inCardPassCheckEvent) {

	}

	@Override
	protected void processInEqubindEvent(InEqubindEvent inEqubindEvent) {

	}

	@Override
	protected void processInEquDataMsg(InEquDataMsg inEquDataMsg) {

	}

}
